import re

from PIL import Image, ImageDraw, ImageFont
import random
import os
import relic_generator
from utils import image_util
import relic_train_img_generator

font_18 = ImageFont.truetype("../font.ttf", size=18)
font_20 = ImageFont.truetype("../font.ttf", size=20)
font_28 = ImageFont.truetype("../font.ttf", size=28)
font_35 = ImageFont.truetype("../font.ttf", size=35)

back = Image.open("images/back.png")


def rand_name() -> str:
    result = ""
    for i in range(8):
        result += str(random.randint(0, 9))
    return result


def get_rand_cut_box(xy: (int, int), box: (int, int, int, int)):
    x0 = random.randint(xy[0] + box[0] - 6, xy[0] + box[0])
    y0 = random.randint(xy[1] + box[1] - 6, xy[1] + box[1])
    x1 = random.randint(xy[0] + box[2], xy[0] + box[2] + 6)
    y1 = random.randint(xy[1] + box[3], xy[1] + box[3] + 6)
    return x0, y0, x1, y1


# 生成文字训练数据集的方法
if __name__ == '__main__':
    for i in range(16000):

        relic0 = relic_generator.generate_relic()
        relic0_img = relic_train_img_generator.generate_relic_img(relic0)
        img = None
        # 要切的两个角坐标
        x0 = None
        y0 = None
        x1 = None
        y1 = None
        # 四个数字0123分别代表圣遗物类别，主词条，副词条，套装名，每次会选择生成一个的训练数据
        type = random.randint(0, 3)
        # type = 1
        text = None

        if type == 0:
            text = relic0.relic_type
            x0, y0, x1, y1 = get_rand_cut_box(xy=(24, 57), box=font_18.getbbox(text))
        elif type == 1:
            text = relic0.main_entry_type
            b0 = font_18.getbbox(text=relic0.main_entry_type.replace("百分比", ""))
            b1 = font_35.getbbox(re.findall(r'\d+\.\d+%|\d+%|\d+\.\d+|\d+', relic0.main_entry_str)[0])
            x0, y0, x1, y1 = get_rand_cut_box(xy=(24, 125), box=(min(b0[0], b1[0]),
                                                                 b0[1],
                                                                 max(b0[2], b1[2]),
                                                                 22 + b1[3]))
        elif type == 2:
            z = random.randint(0, len(relic0.secondary_lemma_type) - 1)
            text = relic0.secondary_lemma_type[z]
            x0, y0, x1, y1 = get_rand_cut_box(xy=(44, 299 + 32 * z),
                                              box=font_20.getbbox(relic0.secondary_lemma_str[z]))

        elif type == 3:
            text = relic0.coordinate_name
            x0, y0, x1, y1 = get_rand_cut_box(xy=(24, 299 + len(relic0.secondary_lemma_type) * 32,),
                                              box=font_20.getbbox(text=text))
        img = relic0_img.crop(box=(x0, y0, x1, y1))
        img = image_util.convert_image(img)
        # img.show()
        src = "../datasets/text_images/train/" + text
        if not os.path.exists(src):
            os.makedirs(src)

        file_name = rand_name()
        while (os.path.exists(src + "/" + file_name + ".png")):
            file_name = rand_name()
        img.save(src + "/" + file_name + ".png")
        print("第" + str(i) + "个路径为" + src + "/" + file_name + ".png" + "的图片生成完了")
# for z in range(2000):
#     # 先随机这个图片是主词条还是副词条
#     is_main_entry = random.randint(0, 1)
#     # is_main_entry = 1
#     # 生成一个圣遗物
#     relic0 = relic_generator.generate_relic()
#     # img = Image.new("RGB", color="#000000", size=(410, 188))
#     # 最后生成的图片
#     img = None
#
#     # 最后生成的图片文件的名字
#     text = None
#
#     if is_main_entry == 1:
#         # 把背景图片粘贴到新生成的图片中（是主词条后面的背景图片，因为副词条后面的是纯色）
#         img = Image.new("RGB", color="#000000", size=(410, 188))
#         img.paste(back)
#         draw = ImageDraw.Draw(img)
#         # 主词条会随机画在背景图片的任意位置上，然后会被截取下来，截取的时候会模拟目标检测画的框的不规则性
#         b0 = font_18.getbbox(relic0.main_entry_type.replace("百分比", ""))
#         b1 = font_35.getbbox(re.findall(r'\d+\.\d+%|\d+%|\d+\.\d+|\d+', relic0.main_entry_str)[0])
#         h = 22 + b1[3] - b0[1]
#         w = max(font_18.getlength(relic0.main_entry_type),
#                 font_35.getlength(re.findall(r'\d+\.\d+%|\d+%|\d+\.\d+|\d+', relic0.main_entry_str)[0])) \
#             - min(b0[0], b1[0])
#         x = random.randint(-b0[0], back.width - w - b0[0])
#         y = random.randint(-b0[1], back.height - h - b0[1])
#
#         # 这四个坐标是从img里面切下来的文本位置的坐标
#         x0 = random.randint(max(x + b0[0] - 10, 0), x + b0[0])
#         y0 = random.randint(max(y + b0[1] - 10, 0), y + b0[1])
#         x1 = random.randint(x + b0[0] + w, min(x + b0[0] + w + 10, img.width))
#         y1 = random.randint(y + b0[1] + h, min(y + b0[1] + h + 10, img.height))
#
#         draw.text(xy=(x, y), text=relic0.main_entry_type.replace("百分比", ""), fill="#bdaea9", font=font_18)
#         draw.text(xy=(x, y + 22)
#                   , text=re.findall(r'\d+\.\d+%|\d+%|\d+\.\d+|\d+', relic0.main_entry_str)[0]
#                   , font=font_35)
#         text = relic0.main_entry_type
#         img = img.crop(box=(x0, y0, x1, y1))
#
#     else:
#         i = random.randint(0, len(relic0.secondary_lemma_type) - 1)
#         text = relic0.secondary_lemma_type[i]
#         draw_text = relic0.secondary_lemma_str[i]
#         box = font_20.getbbox(text=draw_text)
#
#         img = Image.new(mode="RGB", color="#e5ded3",
#                         size=(random.randint(box[2], box[2] + 15), random.randint(box[3], box[3] + 15)))
#
#         img = Image.new(mode="RGB", color="#e5ded3",
#                         size=(random.randint(box[2], box[2]), random.randint(box[3], box[3])))
#         draw = ImageDraw.Draw(img)
#
#         x = random.randint(-box[0], img.width - box[2])
#         y = random.randint(-box[1], img.height - box[3])
#         print(draw_text)
#         draw.text(xy=(x, y), text=draw_text, fill="#495366", font=font_20)
#
#     img = image_util.convert_image(img)
#     # img.show()
#     # text = "防御力"
#     src = "../datasets/text_images/" + text
#     if not os.path.exists(src):
#         os.makedirs(src)
#
#     file_name = rand_name()
#     while (os.path.exists(src + "/" + file_name + ".png")):
#         file_name = rand_name()
#     img.save(src + "/" + file_name + ".png")
#     print("第" + str(z) + "个路径为" + src + "/" + file_name + ".png" + "的图片生成完了")
